package jsmc.opendata.pratikanant.activity;


import greendroid.widget.ActionBarItem;
import greendroid.widget.NormalActionBarItem;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import jsmc.android.object.DialogButton;
import jsmc.opendata.nantes.exceptions.ApiReseauException;
import jsmc.opendata.nantes.modele.Parking;
import jsmc.opendata.nantes.util.EquipementManager;
import jsmc.opendata.nantes.util.NeoLog;
import jsmc.opendata.pratikanant.Constants;
import jsmc.opendata.pratikanant.MainGDActivity;
import jsmc.opendata.pratikanant.R;
import jsmc.opendata.pratikanant.adapter.ListParkings;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class ParkingsActivity extends MainGDActivity {
	/** Called when the activity is first created. */

	private ListView liste = null;
	public  List<Parking> p = null;
	final Handler mHandler = new Handler();

	private static final int ACTION_SEARCH = 1;
	private boolean search_bar_visible = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setActionBarContentView(R.layout.parking_list);

		Thread mSplashThread =  new Thread(){
			@Override
			public void run(){

				p = getParkings();
				mHandler.post(mUpdateResults);

			}
		};

		mSplashThread.start();
		ShowProgress(getString(R.string.dialog_connexion));

		prepareActionBar();

	}

	protected void prepareActionBar() {

		ActionBarItem item = getActionBar().newActionBarItem(NormalActionBarItem.class);
		item.setDrawable(R.drawable.gd_action_bar_search).setContentDescription(R.string.action_search);
		addActionBarItem(item,ACTION_SEARCH);

	}

	@Override
	public boolean onHandleActionBarItemClick(ActionBarItem item, int position) {
		NeoLog.v("LB ParkingActivity","onHandleActionBarItemClick position:"+position);

		switch (item.getItemId()) {
		case ACTION_SEARCH:
			NeoLog.v("LB ParkingActivity","onHandleActionBarItemClick search");

			LinearLayout search_bar = (LinearLayout) findViewById(R.id.parking_search_bar);
			if(!search_bar_visible)
				search_bar.setVisibility(LinearLayout.VISIBLE);
			else 
				search_bar.setVisibility(LinearLayout.GONE);
			search_bar_visible=!search_bar_visible;
			break;
		default:
			return super.onHandleActionBarItemClick(item, position);
		}
		return true;
	}

	private void ShowList(){

		if(p!=null){

			class CustomComparator implements Comparator<Parking> {

				public int compare(Parking arg0, Parking arg1) {
					return arg0.getNom().compareTo(arg1.getNom());
				}
			}

			Collections.sort(p, new CustomComparator());

			ListParkings adapter = new ListParkings(this);
			adapter.SetParkings(p);
			liste = (ListView)  findViewById(R.id.parking_list_view);
			liste.setAdapter(adapter);

			liste.setOnItemClickListener( new OnItemClickListener() {

				public void  onItemClick  (AdapterView<?> parent, View v, int position, long id) {

					Parking parking = p.get(position);
					NeoLog.v("LB ParkingActivity","ShowList pos:"+position+" id:"+position+" nom:"+parking.getNom());			

					Intent intent = new Intent(getApplicationContext(), ParkingsDetailsActivity.class);
					intent.putExtra("IdObj", parking.getIdObj());

					startActivity(intent);


				}

			});

		} else {

			DialogButton PositiveButton = new DialogButton("OK", true);
			DialogButton NegativeButton = null;
			this.CreateDialog("Erreur", "Impossible de se connecter", 
					PositiveButton, 
					NegativeButton);

		}
	}


	final Runnable mUpdateResults = new Runnable() {
		public void run() {

			CloseProgress();

			ShowList();
		}
	};

	private List<Parking> getParkings(){

		List<Parking> p = null;
		try {

			p = app.op.getParkings(Constants.use_db);

		} catch (ApiReseauException e) {

			e.printStackTrace();
			NeoLog.v("OP MainApp","Impossible de lire les data: "+ e.getMessage());

		}

		return p;
	}
}